package com.droid.library.widget.recyclerview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;

import com.droid.library.app.App;
import com.xinye.lib.R;


/**
 * 上拉加载RecyclerView
 *
 * @author wangheng
 */
public class RefreshableRecyclerView extends RecyclerView {

    private static final String TAG = "RefreshableRecyclerView";
    private static final boolean DEBUG = App.getInstance().isDebug();

    private LinearLayoutManager mLayoutManager;

    public RefreshableRecyclerView(Context context) {
        super(context);
        init(context,null);
    }

    public RefreshableRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public RefreshableRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context,attrs);
    }

    private int dividerColor = Color.TRANSPARENT;
    private int dividerSize = 0;

    private void init(Context context, AttributeSet attrs) {
        if(attrs != null){
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RefreshableRecyclerView);
            dividerColor = ta.getColor(R.styleable.RefreshableRecyclerView_dividerColor, Color.TRANSPARENT);
            dividerSize = ta.getDimensionPixelOffset(R.styleable.RefreshableRecyclerView_dividerSize,0);
            ta.recycle();
        }

        mLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL,false);
        super.setLayoutManager(mLayoutManager);

        setItemAnimator(new DefaultItemAnimator());

        if(dividerSize > 0){
            addItemDecoration(new DividerDecoration(context, dividerColor, dividerSize));
        }

        addOnScrollListener(new RefreshOnScrollListener());
    }


    /**
     *  不可以设置LayoutManger
     *
     * @param layout layoutManager
     */
    @Deprecated
    @Override
    public void setLayoutManager(LayoutManager layout) {
//        super.setLayoutManager(layout);
    }

    private class RefreshOnScrollListener extends RecyclerView.OnScrollListener{
        int visibleItemCount;
        int firstVisibleItem;
        int totalItemCount;
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if(DEBUG){
                String stateMeans = "INVALID STATE";
                if(newState == SCROLL_STATE_DRAGGING){
                    stateMeans = "SCROLL_STATE_DRAGGING";
                }else if(newState == SCROLL_STATE_SETTLING){
                    stateMeans = "SCROLL_STATE_SETTLING";
                }else if(newState == SCROLL_STATE_IDLE){
                    stateMeans = "SCROLL_STATE_IDLE";
                }
                Log.i(TAG,"onScrollStateChanged:" + stateMeans);
            }

//            int visibleItemCount = recyclerView.getChildCount();
//            int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
//            int totalItemCount = mLayoutManager.getItemCount();
//
//
//            boolean isLastItem = firstVisibleItem + visibleItemCount  == totalItemCount;
//            if(DEBUG){
//                Log.i(TAG,"visibleItemCount:" + visibleItemCount
//                        + ",firstVisibleItem:" + firstVisibleItem
//                        + ",totalItemCount:" + totalItemCount
//                        + ",isLastItemVisible:" + isLastItem);
//            }
//
//            if(newState == SCROLL_STATE_IDLE && isLastItem && totalItemCount != 0){
//                // 上拉加载
//                if(mListener != null){
//                    mListener.onPullUpToLoading();
//                }
//            }

        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            visibleItemCount = recyclerView.getChildCount();
            firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
            totalItemCount = mLayoutManager.getItemCount();


            boolean isLastItem = firstVisibleItem + visibleItemCount  == totalItemCount;
            if(DEBUG){
                Log.i("wangheng","visibleItemCount:" + visibleItemCount
                        + ",firstVisibleItem:" + firstVisibleItem
                        + ",totalItemCount:" + totalItemCount
                        + ",isLastItemVisible:" + isLastItem + ",dy:" + dy);
            }

            if(dy > 0 && isLastItem && totalItemCount != 0 && !isLoading){
                // 上拉加载
                if(mListener != null){
                    mListener.onPullUpToLoading();
                    isLoading = true;
                }
            }
        }
    }

    public boolean isLastItemVisible(){
        int visibleItemCount = getChildCount();
        int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
        int totalItemCount = mLayoutManager.getItemCount();
        return firstVisibleItem + visibleItemCount  == totalItemCount;
    }

    private OnPullUpToLoadingListener mListener;

    public void setOnPullUpToLoadingListener(OnPullUpToLoadingListener listener){
        mListener = listener;
    }

    public interface OnPullUpToLoadingListener{
        void onPullUpToLoading();
    }

    public boolean isLoading = false;

    public void setIsLoading(boolean isLoading){
        this.isLoading = isLoading;
    }

}
